﻿using System;
using System.Collections.Generic;
using System.Text;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.BLL
{
    public class BLL_Interface : MarshalByRefObject, HZ.DrugTrade.IBLL.IBLL_Interface
    {
        /// <summary>
        /// 增加对照接口
        /// </summary>
        /// <param name="HospitalInterfaceCorrelatio"></param>
        /// <returns></returns>
        public int AddHospitalInterfaceCorrelation(HZ.DrugTrade.Model.HospitalInterfaceCorrelatio HospitalInterfaceCorrelation)
        {
            BLL_UserSession.CheckSession();
            return HZ.Data.DAL_Helper.OperateTable(HospitalInterfaceCorrelation, HZ.Data.TableOperator.Insert);
        }
        /// <summary>
        /// 删除一个对应接口
        /// </summary>
        /// <param name="HospitalInterfaceCorrelatio"></param>
        /// <returns></returns>
        public int DeleteHospitalInterfaceCorrelation(HZ.DrugTrade.Model.HospitalInterfaceCorrelatio HospitalInterfaceCorrelation)
        {
            BLL_UserSession.CheckSession();
            HZ.Common.ThrowHelper.Throw(HospitalInterfaceCorrelation.HISDrugID != null, "删除编号不能空");
            int result =DAL.DAL_Interface.DeleteHospitalInterfaceCorrelation(HospitalInterfaceCorrelation);
            if (result>0)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog() { OperateMessage = string.Format("删除接口", HospitalInterfaceCorrelation.HISDrugID) });
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="HospitalProcureCatalog"></param>
        /// <param name="CurrentPage"></param>
        /// <param name="PageSize"></param>
        /// <param name="TotalRecord"></param>
        /// <param name="similarfactor"></param>
        /// <param name="IntelligenceSearch"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> QueryServerProcureCatalog(HZ.DrugTrade.Model.Query.HospitalProcureCatalog HospitalProcureCatalog, int CurrentPage, int PageSize, out int TotalRecord, double similarfactor, bool IntelligenceSearch)
        {
            BLL_UserSession.CheckSession();
            HZ.Common.ThrowHelper.Throw(HospitalProcureCatalog.GoodsName != null, "产品名称不能为空");
            if (IntelligenceSearch)
            {
                if (similarfactor < 0.5)
                {
                    throw new ArgumentException("相似度最好在0.5以上", "similarfactor");
                }
                HospitalProcureCatalog.SortFields = "";
                return DAL.DAL_Interface.QueryServerProcureCatalogIntelligence(HospitalProcureCatalog, CurrentPage, PageSize, out TotalRecord, similarfactor);

            }
            else
            {
                HospitalProcureCatalog.Append = string.Format("ProcureCatalogID NOT IN (SELECT ProcureCatalogID FROM TB_HospitalInterfaceCorrelation WHERE HospitalID = '{0}')", HospitalProcureCatalog.HospitalID);
                return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.HospitalProcureCatalog, HZ.DrugTrade.Model.Query.HospitalProcureCatalog>(HospitalProcureCatalog, CurrentPage, PageSize, out TotalRecord);
            }


            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取对应好的接口
        /// </summary>
        /// <param name="where"></param>
        /// <param name="CurrentPage"></param>
        /// <param name="PageSize"></param>
        /// <param name="RecordCount"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.HospitalInterface> GetMatchedHospitalInterface(HZ.DrugTrade.Model.Query.HospitalInterface where, int CurrentPage, int PageSize, out int RecordCount)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.GetMatchedHospitalInterface(where, CurrentPage, PageSize, out RecordCount);
        }

        public IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> GetUnMatchedHospitalInterface(HZ.DrugTrade.Model.Query.HospitalProcureCatalog where, int CurrentPage, int PageSize, out int RecordCount)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.GetUnMatchedHospitalInterface(where, CurrentPage, PageSize, out RecordCount);
        }


        public int AddCompanyInterfaceCorrelation(CompanyInterfaceCorrelation CompanyInterfaceCorrelation)
        {
            BLL_UserSession.CheckSession();
            return HZ.Data.DAL_Helper.OperateTableSQL(CompanyInterfaceCorrelation, HZ.Data.TableOperator.Insert);
        }

        public int DeleteCompanyInterfaceCorrelation(CompanyInterfaceCorrelation CompanyInterfaceCorrelation)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.DeleteCompanyInterfaceCorrelation(CompanyInterfaceCorrelation);
        }

        public IList<HZ.DrugTrade.Model.View.ProcureCatalog> QueryServerProcureCatalog(HZ.DrugTrade.Model.Query.ProcureCatalog where, int CurrentPage, int PageSize, out int TotalRecord, double similarfactor, bool IntelligenceSearch)
        {
            BLL_UserSession.CheckSession();
            if (IntelligenceSearch)
            {

                if (similarfactor < 0.5)
                {
                    throw new ArgumentException("相似度最好在0.5以上", "similarfactor");
                }

                return DAL.DAL_Interface.QueryServerProcureCatalogIntelligence_Company(where, CurrentPage, PageSize, out TotalRecord, similarfactor);
            }
            else
            {
                return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.ProcureCatalog, HZ.DrugTrade.Model.Query.ProcureCatalog>(where, CurrentPage, PageSize, out TotalRecord);
            }
            throw new NotImplementedException();
        }




        public IList<HZ.DrugTrade.Model.View.CompanyInterface> GetMatchedCompanyInterface(HZ.DrugTrade.Model.Query.CompanyInterface where, int CurrentPage, int PageSize, out int RecordCount)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.GetMatchedCompanyInterface(where, CurrentPage, PageSize, out RecordCount);
        }






        #region IBLL_Interface Members


        public IList<HZ.DrugTrade.Model.View.ImportedPurchaseOrderDetail> MatchPurchaseMemu(IList<ImportPurchaseOrderDetail> purchase, string AccountID, out IList<ImportPurchaseOrderDetail> UnMatchedPurchase)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.MatchPurchaseMemu(purchase, AccountID, out UnMatchedPurchase);
        }

        /// <summary>
        /// 自动对应接口数据
        /// </summary>
        /// <param name="CorrelationList">客户端待对应数据列表</param>
        /// <param name="UnCorrelationList">未对应数据列表</param>
        /// <returns>1成功 0失败</returns>
        public int AutoCorresponding(IList<HospitalInterfaceCorrelatio> CorrelationList, out IList<HospitalInterfaceCorrelatio> UnCorrelationList)
        {
            BLL_UserSession.CheckSession();
            if (CorrelationList == null)
            {
                throw new ArgumentNullException("CorrelationList");
            }
            if (CorrelationList.Count == 0)
            {
                throw new ArgumentException("列表数目必须大于0", "CorrelationList");
            }
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(CorrelationList[0].HospitalID), "医疗机构ID不能空");
            return DAL.DAL_Interface.AutoCorresponding(CorrelationList, out UnCorrelationList);
        }


        public int AutoCorresponding_Company(IList<CompanyInterfaceCorrelation> CorrelationList, out IList<CompanyInterfaceCorrelation> UnCorrelationList)
        {
            BLL_UserSession.CheckSession();
            if (CorrelationList == null)
            {
                throw new ArgumentNullException("CorrelationList");
            }
            if (CorrelationList.Count == 0)
            {
                throw new ArgumentException("列表数目必须大于0", "CorrelationList");
            }
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(CorrelationList[0].CompanyID), "企业ID不能空");
            return DAL.DAL_Interface.AutoCorresponding_Company(CorrelationList, out UnCorrelationList);
            throw new NotImplementedException();
        }



        public IList<Model.View.CompanyExportPuchaseOrderDetail> GetCorrespondingPurchaseOrderDetail_Company(HZ.DrugTrade.Model.Query.CompanyExportPuchaseOrderDetail where, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<Model.View.CompanyExportPuchaseOrderDetail, Model.Query.CompanyExportPuchaseOrderDetail>(where, currentpage, pagesize, out totalrecord);
        }




        #endregion

        #region IBLL_Interface Members


        public IList<HZ.DrugTrade.Model.View.CompanyProcureCatalog> GetCompanyProcureCatalog(HZ.DrugTrade.Model.Query.CompanyProcureCatalog where, int CurrentPage, int PageSize, out int TotalRecord)
        {
            BLL_UserSession.CheckSession();
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<HZ.DrugTrade.Model.View.CompanyProcureCatalog, HZ.DrugTrade.Model.Query.CompanyProcureCatalog>(where, CurrentPage, PageSize, out TotalRecord);
            throw new NotImplementedException();
        }

        public IList<HZ.DrugTrade.Model.View.CompanyProcureCatalog> GetUnMatchedCompanyProcureCatalog(HZ.DrugTrade.Model.Query.CompanyProcureCatalog where, int CurrentPage, int PageSize, out int TotalRecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_Interface.GetUnMatchedCompanyProcureCatalog(where, CurrentPage, PageSize, out TotalRecord);
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取医疗机构已入库信息
        /// (有医疗机构编码)
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currenpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<Model.View.HopitalInflow_Export> GetHopitalInflow_Export(Model.Query.HopitalInflow_Export where, int currenpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return HZ.DrugTrade.DAL2.DAL_Helper.GetPagedView<Model.View.HopitalInflow_Export, Model.Query.HopitalInflow_Export>(where, currenpage, pagesize, out totalrecord);
        }

        #endregion

        #region IBLL_Interface Members


        public IList<HZ.DrugTrade.Model.Interface.ERPDistribution> ImportDistribution(IList<HZ.DrugTrade.Model.Interface.ERPDistribution> erplist, PurchaseOrder PurchaseOrder, string companyID, out IList<HZ.DrugTrade.Model.Interface.ERPDistribution> UnMatchedERPDistribution)
        {
            BLL_UserSession.CheckSession();
            if (PurchaseOrder == null)
            {
                throw new ArgumentNullException("PurchaseOrder");
            }
            if (PurchaseOrder.OrderID == null)
            {
                throw new ArgumentException("OrderID不能空", "PurchaseOrder");
            }


            return DAL.DAL_Interface.ImportDistribution(erplist, PurchaseOrder, companyID, out UnMatchedERPDistribution);
            throw new NotImplementedException();
        }

        #endregion
    }
}
